package com.twotigers.tenthpower.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.twotigers.tenthpower.entity.TbCity;
import com.twotigers.tenthpower.mapper.TbCityMapper;
import com.twotigers.tenthpower.service.ITbCityService;
import io.renren.common.utils.IdWorker;
import io.renren.common.utils.Result;
import io.renren.common.utils.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 城市 服务实现类
 * </p>
 *
 * @author ice
 * @since 2021-03-05
 */
@Service
public class TbCityServiceImpl extends ServiceImpl<TbCityMapper, TbCity> implements ITbCityService {

    @Autowired
    private IdWorker idWorker;

    //按 id 查询
    @Override
    public TbCity findOne(String id) {
        return baseMapper.selectById(id);
    }

    //根据条件查询城市列表
    @Override
    public List<TbCity> find(TbCity city) {
        QueryWrapper<TbCity> qw = new QueryWrapper<>();
        if (city.getId() != null && !city.getId().equals("")) {
            qw.eq("id", city.getId());
        }
        if (city.getName() != null && !city.getName().equals("")) {
            qw.like("name", city.getName());
        }
        if (city.getIshot() != null && !city.getIshot().equals("")) {
            qw.eq("ishot", city.getIshot());
        }
        return baseMapper.selectList(qw);
    }

    //根据条件查询城市列表 + 分页
    @Override
    public IPage<TbCity> fenye(TbCity city, int page, int size) {
        QueryWrapper<TbCity> qw = new QueryWrapper<>();
        IPage<TbCity> ipage = new Page<>(page,size);
        if (city.getId() != null && !city.getId().equals("")) {
            qw.eq("id", city.getId());
        }
        if (city.getName() != null && !city.getName().equals("")) {
            qw.like("name", city.getName());
        }
        if (city.getIshot() != null && !city.getIshot().equals("")) {
            qw.eq("ishot", city.getIshot());
        }

        return baseMapper.selectPage(ipage, qw);
    }

}
